<?php


namespace app\cms\tags;

use app\cms\model\carouselModel;
use app\cms\model\formModel;
use app\cms\model\friendModel;
use app\cms\model\navModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

class models {

    /**
     * 获取单表数据
     * @param $form_id
     * @param mixed $groupId
     * @param mixed $id
     * @param array $options
     * @return array
     * @throws DbException
     */
    public function getData($form_id, mixed $groupId = '', mixed $id = '', array $options = [
        'order' => 'create_time@desc',
        'pageSize' => 15
    ]): array {
        $form = formModel::getForm($form_id);
        if ($form) {
            [$f, $d] = explode('@', $options['order']);
            $where = $options['where'] ?? [];
            $where['status'] = 1;

            if ($id !== '') $where['id'] = $id;
            $query = Db::name($form['form_name']) -> where($where) -> order([
                $f => $d
            ]) -> paginate([
                'list_rows' => $options['pageSize']
            ]);
            return [
                'items' => $query -> items(),
                'count' => $query -> total(),
                'page' => $query -> lastPage()
            ];
        }
        return [];
    }

    /**
     * 模型一对一 关联查询
     * @param $form_id
     * @param string $groupId
     * @param string $id
     * @param array $options
     * @return array
     */
    public function getHasModelData($form_id, $groupId = '', $id = '', array $options = [
        'order' => 'create_time@desc',
        'pageSize' => 10
    ]): array {
        $data = (new formModel()) -> getHasModelDataList($form_id, $groupId, $id, options: $options) -> data;
        return [
            'items' => $data['items'],
            'count' => $data['count'] ?? 0,
            'page' => $data['page'] ?? 0,
            'form_id' => $form_id,
        ];
    }

    /**
     * 获取绑定模型内容
     * @param $form_id
     * @param string $keyword
     * @return array
     */
    public function getBindModelData($form_id, string $keyword = ''): array
    {
        return (new formModel()) -> getBindModelData($form_id, $keyword) -> data['items'];
    }

    /**
     * 获取导航栏信息
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getNav(): array
    {
        return (new navModel) -> getNav() -> data['items'] ?: [];
    }

    /**
     * 获取轮播图
     * @param int $groupId
     * @param int $length
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getCarousel(int $groupId = 1, int $length = 5): array
    {
        return carouselModel::where([
            'carousel_group_id' => $groupId
        ]) -> limit(0, $length) -> select() -> toArray();
    }

    /**
     * 获取指定模型上一条和下一条数据
     * @param int $form_id
     * @param string|int $groupId
     * @param int $offset 第几条数据
     * @return array
     */
    public function getPreAndNext(int $form_id, string|int $groupId = "", int $offset = 0): array
    {
        return (new formModel()) -> getPreAndNext($form_id, $groupId, $offset) -> data['items'];
    }

    /**
     * 获取友链
     * @return array
     */
    public function getFriends(): array {
        return friendModel::getFriend() -> data['items'];
    }
}